<?php

/**
 * 首信易（微信扫码）支付插件
 * ============================================================================
 * * 版权所有  广州多想网络科技公司，并保留所有权利。
 * 网站地址: http://www.duoxiangweb.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: xie $
 * $Id: payeaseWeixin.php  $
 */
 

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$payment_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/payment/cappay.php';

if (file_exists($payment_lang))
{
    global $_LANG;

    include_once($payment_lang);
}

/**
 * 模块信息
 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code']    = 'payeaseWeixin';

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'payeaseWeixin_desc';

    /* 是否支持货到付款 */
    $modules[$i]['is_cod']  = '0';

    /* 是否支持在线支付 */
    $modules[$i]['is_online']  = '1';

    /* 作者 */
    $modules[$i]['author']  = 'xie';

    /* 网址 */
    $modules[$i]['website'] = 'http://www.beijing.com.cn';

    /* 版本号 */
    $modules[$i]['version'] = 'V1.0';

    /* 配置信息 */
    $modules[$i]['config'] = array(
        array('name' => 'cappay_account',  'type' => 'text',   'value' => ''),
        array('name' => 'cappay_key',      'type' => 'text',   'value' => '')
    );

    return;
}
class payeaseWeixin
{
    /**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
	function __construct()
	{
		$this->payeaseWeixin();
	}
	
    function payeaseWeixin()
    {
    }

    /**
     * 生成支付代码
     * @param   array   $order      订单信息
     * @param   array   $payment    支付方式信息
     */
    function get_code($order = array(), $payment = array())
    {
    	$v_ymd = local_date('Ymd',$order['add_time']); //订单产生日期，要求订单日期格式yyyymmdd.
    	$v_mid = trim($payment['cappay_account']);    //商户编号，和首信签约后获得,测试的商户编号444
    	$v_rcvname = $order['order_sn']; //收货人姓名,建议用商户编号代替或者是英文数字。因为首信平台的编号是gb2312的
    	$v_rcvaddr = $v_mid; //收货人地址，可用商户编号代替
    	$v_rcvtel = $order['mobile'];   //收货人电话
    	$v_rcvpost = $v_mid;  //收货人邮箱
    	$v_amount = $order['order_amount']; //订单金额
//     	$v_amount = '0.01'; //订单金额
    	$v_orderstatus = "1";//配货状态:0-未配齐，1-已配
    	$v_ordername = $v_mid;  //订货人姓名
    	$v_moneytype = "0";  //0为人民币，1为美元，2为欧元，3为英镑，4为日元，5为韩元，6为澳大利亚元，7为卢布(内卡商户币种只能为人民币)
    	$v_url = "http://www.drleawell.com";
    	$v_producttype = urlencode("货物贸易");
    	$v_idtype = "01";
    	$v_idnumber = urlencode($order['credit_card']);
    	$v_idname = urlencode($order['consignee']);
    	$v_idcountry = "156";
    	$v_idaddress = '';
    	$v_userref = urlencode($_SESSION['user_id']);
    	$v_ordip = '119.29.141.135';
//     	$v_ordip = '122.114.226.194';
    	$v_oid = $v_ymd .'-' . $v_mid . '-' .local_date('His').'-'.$order['order_id'];
    	$v_merdata5 = urlencode($v_oid);
    	$MD5Key = trim($payment['cappay_key']);
    	
    	$data = $v_moneytype.$v_ymd.$v_amount.$v_rcvname.$v_oid.$v_mid.$v_url;//七个参数的拼串
    	$v_md5info = $this->hmac_md5($MD5Key, $data);
    	
        $curlPost = '';
    	$url = "http://pay.yizhifubj.com/customer/otherpay/scanCodePay.jsp";
    	$curlPost.="v_mid=".$v_mid;
    	$curlPost.="&v_oid=".$v_oid;
    	$curlPost.="&v_rcvname=".$v_rcvname;
    	$curlPost.="&v_rcvaddr=".$v_rcvaddr;
    	$curlPost.="&v_rcvtel=".$v_rcvtel;
    	$curlPost.="&v_rcvpost=".$v_rcvpost;
    	$curlPost.="&v_amount=".$v_amount;
    	$curlPost.="&v_ymd=".$v_ymd;
    	$curlPost.="&v_orderstatus=".$v_orderstatus;
    	$curlPost.="&v_ordername=".$v_ordername;
    	$curlPost.="&v_moneytype=".$v_moneytype;
    	$curlPost.="&v_url=".$v_url;
    	$curlPost.="&v_ordip=".$v_ordip;
    	$curlPost.="&v_md5info=".$v_md5info;
    	$curlPost.="&v_merdata5=".$v_merdata5;
    	$curlPost.="&v_producttype=".$v_producttype;
    	$curlPost.="&v_idtype=".$v_idtype;
    	$curlPost.="&v_idnumber=".$v_idnumber;
    	$curlPost.="&v_idname=".$v_idname;
    	$curlPost.="&v_idcountry=".$v_idcountry;
    	$curlPost.="&v_idaddress=".$v_idaddress;
    	$curlPost.="&v_userref=".$v_userref;
//     	return $curlPost;
    	
    	$ch=curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    	$back_date=trim(curl_exec($ch));
    	curl_close($ch);
    	$xml = simplexml_load_string($back_date);
//     	var_dump($xml);
		$def_url = '';
		if ($xml->status == 0){
			$codeurl = $xml->codeurl;
			$bankurl = $xml->bankurl;
			 
			$pic = file_get_contents($bankurl);
			$filename = ROOT_PATH.'images/qrcode/'.$v_oid.'.jpg';
	    	if(file_put_contents( $filename,$pic)){
	    		$img = str_replace(ROOT_PATH,'',$filename).'?num='.rand(00000,99999);
	    		$def_url .= '<div style="width: 100%;text-align:center;">';
	    		$def_url .= '<p style="font-size: 16px;color: red;">请使用微信扫码完成支付，支付成功后请耐心等待</p>';
	    		$def_url .= '<img src="'.$img.'" style="width: 30%;">';
	    		$def_url .= '</div>';
	    		$def_url .= '<script type="text/javascript">setInterval(function(){';
	    		$def_url .= '$.post("flow.php?step=wx_check_order",{ order_id:'.$order['order_id'].'},function(res){';
	    		$def_url .= '	if(res.error == 0) window.location.href="respond.php?code=payeaseWeixin";';
	    		$def_url .= '},"JSON")';
	    		$def_url .= '},2000);</script>';
	    		
	    		//修改支付单号
	    		$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info').
	    		" SET ".
	    		" pay_sn='$v_oid' ".
	    		" WHERE order_id=".$order['order_id'];
	    		$GLOBALS['db']->query($sql);
	    		
	    	}else{
	    		$def_url .= '<p style="font-size: 16px;color: red;">获取二维码出错，请联系管理员。</p>';
	    	}
	    	
		}else{
			$def_url .= '<p style="font-size: 16px;color: red;">错误信息：'.$xml->statusdesc.'。请在会员中心“我的反馈”联系管理员。</p>';
		}
    	

        return $def_url;
    }
    
    function respond()
    {
    	return true;
    }

    function hmac_md5($key, $data)
    {
        if (extension_loaded('mhash'))
        {
            return bin2hex(mhash(MHASH_MD5, $data, $key));
        }

        // RFC 2104 HMAC implementation for php. Hacked by Lance Rushing
        $b = 64;
        if (strlen($key) > $b)
        {
            $key = pack('H*', md5($key));
        }
        $key  = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));

        $k_ipad = $key ^ $ipad;
        $k_opad = $key ^ $opad;

        return md5($k_opad . pack('H*', md5($k_ipad . $data)));
    }
    

}

?>